package com.redlion.weapon;

import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.RotationModifier;
import org.andengine.entity.sprite.AnimatedSprite.IAnimationListener;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.util.Vector2Pool;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.util.math.MathUtils;
import org.andengine.util.modifier.ease.EaseLinear;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.redlion.Game;
import com.redlion.hud.MyHUD.SLOT;
import com.redlion.object.Planet;
import com.redlion.object.Shot;
import com.redlion.object.ShotPool;

public class Cannon extends AbstractAnimationWeapon implements IPlanetWeapon {

	private static final float SHOT_OFFSET = 50;
	private ShotPool mBullets;
	public OnFireListener shotListener;
	public IEntityModifier rotation;
	public IdHolder mIdHolder;
	public Vector2 dir;

	public Cannon(Vector2 position, ITiledTextureRegion textureRegion, FixtureDef fixDef) {
		super(position, textureRegion, fixDef);
		hide();
	}

	public Cannon(Vector2 position, ITiledTextureRegion textureRegion) {
		super(position, textureRegion);
		hide();
	}

	@Override
	protected void createAnimation() {

	}

	public void alignToPlanet(Planet p, float offset) {
		sprite.setPosition(p.centerX + offset - (sprite.getWidth() / 2), p.centerY - (sprite.getHeight() / 2));
		sprite.setRotationCenterX(-offset + (sprite.getWidth() / 2));
	}

	@Override
	protected void createAnimation(float px, float py) {
		sprite = new AnimatedSprite(px - (mTiledTextureRegion.getWidth() / 2), py - (mTiledTextureRegion.getHeight() / 2),
				mTiledTextureRegion, Game.gameActivity.getVertexBufferObjectManager());
	}

	@Override
	protected void createBody(FixtureDef fixtureDef) {
		body = PhysicsFactory.createCircleBody(Game.mPhysicsWorld, sprite, BodyType.DynamicBody, fixtureDef);
		body.setUserData(getClass());
	}

	public void setBullets(ShotPool bullets) {
		this.mBullets = bullets;
	}

	@Override
	public void setFireListener(OnFireListener listener) {
		this.shotListener = listener;
	}

	@Override
	public void fire() {
		if (shotListener != null) {
			shotListener.firePlanetCannon(this);
			// final LaserStroke line = new LaserStroke(start.x, start.y, end.x,
			// end.y, 2,
			// GameController.gameActivity.getVertexBufferObjectManager());

			// StartScreenActivity.mScene.attachChild(line);
			// Vector2Pool.recycle(end);
			// Vector2Pool.recycle(start);
			// Vector2Pool.recycle(dir);
			// Shot b = mBullets.obtainPoolItem();
			// Shot b = new Shot(this.getShotOrigin(false),
			// mBulltes.mTextureRegion.deepCopy(), mBulltes.mFixtureDef);
			// GameController.gameActivity.getEngine().getScene().attachChild(b.sprite);
			// GameController.mPhysicsWorld.registerPhysicsConnector(new
			// PhysicsConnector(b.sprite, b.body, true, true));
			// b.shoot(dir);
			// shotListener.fire(b);
			// }
		}
	}

	public void update(float delta) {
	}

	@Override
	public IAnimationListener getAnimationListener() {
		return new IAnimationListener() {

			@Override
			public void onAnimationStarted(AnimatedSprite pAnimatedSprite, int pInitialLoopCount) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationLoopFinished(AnimatedSprite pAnimatedSprite, int pRemainingLoopCount, int pInitialLoopCount) {
				fire();
			}

			@Override
			public void onAnimationFrameChanged(AnimatedSprite pAnimatedSprite, int pOldFrameIndex, int pNewFrameIndex) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationFinished(AnimatedSprite pAnimatedSprite) {
				// TODO Auto-generated method stub

			}
		};
	}

	public void enableRotation(float duration) {
		rotation = new RotationModifier(duration, 0, 360, EaseLinear.getInstance()) {
			@Override
			protected void onSetValue(final IEntity pEntity, final float pPercentageDone, final float pRotation) {
				if (pPercentageDone == 1) {
					this.reset();
				}
				super.onSetValue(pEntity, pPercentageDone, pRotation);
			}
		};
		sprite.registerEntityModifier(rotation);
	}

	public void enableRotation(IEntityModifier rotation) {
		sprite.registerEntityModifier(rotation);
	}

	public Vector2 getShotOrigin(boolean update) {
		if (dir == null || update) {
			float degAngle = MathUtils.degToRad(sprite.getRotation());
			dir = new Vector2((float) Math.cos(degAngle), (float) Math.sin(degAngle)).nor();
		}
		return Game.planet.center.cpy().add(dir.cpy().mul(Game.planet.raduis + SHOT_OFFSET));
	}

	@Override
	public void hide() {
		sprite.setIgnoreUpdate(true);
		sprite.setVisible(false);
	}

	@Override
	public int getWeaponID() {
		if (mIdHolder != null)
			return mIdHolder.weaponIndex;
		return -1;
	}

	@Override
	public int getUpgradeID() {
		if (mIdHolder != null)
			return mIdHolder.upgradeIndex;
		return -1;
	}

	@Override
	public void updateAnimation(Upgrade upgrade, int[] textureIndices) {
		sprite.setIgnoreUpdate(false);
		sprite.setVisible(true);
		animate(upgrade.delay, textureIndices);
	}

	@Override
	public void setIdHolder(IdHolder userData) {
		mIdHolder = userData;
	}

}
